export default {
  getCurrentSitePos(){
    let data = [];

    if(this.mapClip){
      data = this.mapClip.map.map(item => item.mapPos);
    } else {
      data = this.mapData.world.map(item => {
        return item.worldPos;
      });
    }
    
    return data;
  },
  
  // 移动地图到指定地点
  moveGrid(pos){
    let mapWrapper = this.htmlElement.querySelector('.map-wrapper');
    let mapDom = this.htmlElement.querySelector('.map')
    let grid = this.htmlElement.querySelector('#grid_' + pos.join(''));

    if(!grid) return;
    
    this.moveX = mapWrapper.offsetWidth / 2 - grid.offsetLeft - 100;
    this.moveY = mapWrapper.offsetHeight / 2 - grid.offsetTop - 40;

    mapDom.style.transform = `translate(${this.moveX}px, ${this.moveY}px)`;
  },

  isRenderLine(data){
    if(!data[0] || !data[1]) return true;

    // for(let i = 0; i < this.lines.length; i++){
    //   let line = this.lines[i];

    //   if(line[0][0] == data[1][0] && line[0][1] == data[1][1] && 
    //      line[1][0] == data[0][0] && line[1][1] == data[0][1]){
    //       return true;
    //     }
    // }
    for(let i = 0; i < this.lines.length; i++){
      let line = this.lines[i];

      if(line[0][0] == data[0][0] && line[0][1] == data[0][1] && 
         line[1][0] == data[1][0] && line[1][1] == data[1][1]){
          return true;
        }
    }

    return false;
  },

  isExistsNext(arr, pos){
    return arr.find(item => {
      return item && (item.pos[0] == pos[0] && item.pos[1] == pos[1])
    })
  },

  useEditMode(cb){
    this.editSignal.removeAll();
    this.editSignal.add((...arg) => {
      this.htmlElement.querySelector('.tip').innerHTML = '';
      this.editMode = false;
      cb(...arg);
    });
    this.editMode = true;
  },

  // 需要手动关闭编辑模式
  useMultipleEditMode(cb, arr = []){
    this.activeMaps = [...arr];
    this.editModeCb = cb;
    this.mEditMode = true;
    this.renderView(false);
  },

  // 获取指定世界定位的数据
  getMapClip(pos){
    return this.mapData.world.find(item => {
      return item.worldPos[0] == pos[0] && item.worldPos[1] == pos[1]
    })
  },

  getMapSite(mapClip, pos){
    return mapClip.map.find(item => {
      return item.mapPos[0] == pos[0] && item.mapPos[1] == pos[1]
    })
  },

  getMaxStoreId(){
    let storeId = 0;

    [...this.mapData.npcs, ...this.mapData.goods].map(item => {
      if(item.storeId > storeId) storeId = item.storeId;
    })

    return storeId + 1;
  },

  // 获取需要游戏内生成的npc
  getGenerateNpcs(worldPos){
    return this.mapData.npcs.filter(item => {
      return item.generate 
        && item.peopleAttr.worldPos[0] == worldPos[0]
        && item.peopleAttr.worldPos[1] == worldPos[1]
    })
  },

  // 获取指定地区的npc
  getPosNpcs(worldPos, mapPos){
    return this.mapData.npcs.filter(item => {
      return !item.generate 
        && item.peopleAttr.worldPos[0] == worldPos[0]
        && item.peopleAttr.worldPos[1] == worldPos[1]
        && item.peopleAttr.mapPos[0] == mapPos[0]
        && item.peopleAttr.mapPos[1] == mapPos[1]
    });
  },

  // 获取需要游戏内生成的good
  getGenerateGoods(worldPos){
    return this.mapData.goods.filter(item => {
      return item.generate 
        && item.goodAttr.worldPos[0] == worldPos[0]
        && item.goodAttr.worldPos[1] == worldPos[1]
    })
  },

  // 获取指定地区的good
  getPosGoods(worldPos, mapPos){
    return this.mapData.goods.filter(item => {
      return !item.generate 
        && item.goodAttr.worldPos[0] == worldPos[0]
        && item.goodAttr.worldPos[1] == worldPos[1]
        && item.goodAttr.mapPos[0] == mapPos[0]
        && item.goodAttr.mapPos[1] == mapPos[1]
    });
  },
}